package easy

import (
	. "GoLeetcode/common"
	"strconv"
)

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

var result []string

// 一般这种从根节点拼接到子节点,可以使用一带二递归(父节点传参数到子节点)
func binaryTreePaths(root *TreeNode) []string {
	result = make([]string, 0)
	joinTreePaths(root, "")
	return result
}

func joinTreePaths(root *TreeNode, path string) {
	if root != nil {
		path += strconv.Itoa(root.Val)
		if root.Left == nil && root.Right == nil {
			result = append(result, path)
		} else {
			path += "->"
			// 这里传入的path如果在joinTreePaths()内被修改,是不会对上面的path造成影响的(函数是值传递)
			joinTreePaths(root.Left, path)
			joinTreePaths(root.Right, path)
		}
	}
	return
}
